// pages/user/index.js
const TRADIS = require('../../tradis/tradis.js');
import config from '../../utils/config.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    maskHidden:false,
    pageIn: 'userindex',
    gamelist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    wx.setNavigationBarTitle({
      title: '个人中心',
    });
    // wx.hideShareMenu({
      
    // })
    wx.showShareMenu({
      withShareTicket: true
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    TRADIS.tradis(this);
    this.fetchData();
    this.fetchAD();
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    this.data.detail.user.nickname 
    var shareObj = app.getShareDefault();
    return {
      title: shareObj.sharetxt,
      imageUrl: shareObj.imgurl,
      path: '/pages/index/index',
      success: (res) => {
        wx.showToast({
          title: '分享成功',
          icon: 'none',
        })

      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '分享失败',
          icon: 'none',
        })
      }

    }
  },
  fetchData:function(){
    wx.request({
      url: app.globalData.api + '/users/index.html&token=' + wx.getStorageSync('token'),
      method: 'get',
      success: (res) => {
        if (res.data.code == 0) {
          wx.hideLoading();
          this.setData({
            detail:res.data.data
          })
        }
        else {
          this.fetchError(res.data.code);
        }
      }
    })
  },
  fetchAD: function () {
    wx.request({
      url: app.globalData.api + '/users/ad.html&token=' + wx.getStorageSync('token'),
      method: 'get',
      success: (res) => {
        if (res.data.code == 0) {
          wx.hideLoading();
          this.setData({
            gamelist: res.data.data
          })
        }
        else {
          this.fetchError(res.data.code);
        }
      }
    })
  },
  fetchError: function (code) {
    if (code == 40001) {
      wx.navigateTo({
        url: '/pages/auth/getuserinfo',
      })
    }

  },
  go_question:function(){
    wx.navigateTo({
      url: '/pages/user/questions',
    })
  },
  go_record: function () {
    wx.navigateTo({
      url: '/pages/user/record',
    })
  },
  go_tixian: function () {
    wx.navigateTo({
      url: '/pages/user/withdraw',
    })
  },
  replay:function(){
    wx.redirectTo({
      url: '/pages/index/index',
    })
  },
  go_tousu:function(){
    wx.navigateTo({
      url: '/pages/tousu/tousu',
    })
  },
  go_level:function(){
    wx.navigateTo({
      url: '/pages/user/level',
    })
  },
  go_version:function(){
    wx.navigateTo({
      url: '/pages/user/version',
    })
  },
  createHB: function (e) {
    console.log('user=' + JSON.stringify(this.data.detail.user));
    wx.navigateTo({
      url: '/pages/user/share?user='+JSON.stringify(this.data.detail.user),
    })

  },
  openlv:function(){
    wx.navigateTo({
      url: '/pages/user/level',
    })
  },
  click_count(e) {
    var url = 'https://r.d1fm.com/r.gif?appid=' + e.currentTarget.dataset.appid + '&path=' + encodeURIComponent(e.currentTarget.dataset.path) + '&openid=' + wx.getStorageSync('openid') + '&action=click&xsl_from=fengkuangcaichengyu&xsl_position=' + e.currentTarget.dataset.position;
    console.log(url);
    wx.request({
      url: url
    })
  },
  // 跳转至查看文章详情
  redictDetail: function (e) {
    console.log(e);
    //this.postFormID(e);
    if (e.currentTarget.dataset.linktype == 'h5') {
      wx.navigateTo({
        url: '/tradis/h5/h5?url=' + encodeURIComponent(e.currentTarget.dataset.path)
      })

    }
    else if (e.currentTarget.dataset.linktype == 'wxa') {
      wx.navigateToMiniProgram({
        appId: e.currentTarget.dataset.appid,
        path: e.currentTarget.dataset.path,
        success: (res) => {
          this.click_count(e);
        }
      })
    }
    else if (e.currentTarget.dataset.linktype == 'article') {
      wx.navigateTo({
        url: e.currentTarget.dataset.path
      })

    }
    else {
      wx.navigateTo({
        url: '/pages/ad/detail?id=' + e.currentTarget.id,
      })
    }
  },

  

})